<template>
	<view>
    <!-- search -->
    <my-search @click='gotoSearch'></my-search>
    
		<view class="scroll-container">
      <!-- 左侧scroll -->
		  <scroll-view scroll-y="true" class="scroll-left" :style="{height:wh + 'px'}">
		    <view :class="['scroll-left-item',active===i1?'active':'']" v-for="(item1,i1) in cateList" :key='i1' @click="onLeftItemClick(i1)">
		      {{item1.cat_name}}
		    </view>
		  </scroll-view>
      
      <!-- 右侧scroll -->
      <scroll-view scroll-y="true" class="scroll-right" :style="{height:wh + 'px'}" :scroll-top = 'sceollTop'>
        <block v-for="(item2,i2) in cateListLv2" :key='i2'>
          <view class="lv2-title">
            / {{item2.cat_name}} /
          </view>
          <view class="lv3-box">
            <navigator class="lv3-item" v-for="(item3,i3) in item2.children" :key='i3' :url="'/subpkg/goods_list/goods_list?cat_id='+ item3.cat_id ">
              <image :src="item3.cat_icon"></image>
              <text>{{item3.cat_name}}</text>
            </navigator>
          </view>
        </block>
      </scroll-view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				cateList:[],
        // 二级列表
        cateListLv2:[],
        active:0,
        // 屏幕可用高度
        wh:0,
        sceollTop:0
			}
		},
    onLoad(){
      this.getCateList()
      const sysInfo = uni.getSystemInfoSync()
      this.wh = sysInfo.windowHeight + 50
    },
    methods:{
      async getCateList(){
        const {data: res} = await uni.$http.get('/api/public/v1/categories')
        if(res.meta.status!==200) return uni.$message()
        // 存储二级列表
        this.cateListLv2 = res.message[this.active].children
        
        this.cateList = res.message
      },
      onLeftItemClick(index) {
        this.active = index
        this.cateListLv2 = this.cateList[this.active].children
        this.sceollTop = this.sceollTop === 1 ? 0 : 1
      },
      gotoSearch() {
        uni.navigateTo({
          url:'/subpkg/search/search'
        })
      }
    }
	}
</script>

<style lang="scss">
  .scroll-container {
    display: flex;
  }
  .scroll-left {
    width: 90px;
  }
.scroll-left-item {
  position: relative;
  width: 90px;
  text-align: center;
  line-height: 50px;
  height: 50px;
  background-color: #F7F7F7;
  font-size: 12px;
  &.active {
    background-color: #fff;
    &::before {
      content: ' ';
      display: block;
      width: 3px;
      height: 50%;
      background-color: #c00000;
      position: absolute;
      top: 50%;
      left: 0px;
      transform: translateY(-50%);
    }
  }
}
.scroll-right {
  .lv2-title {
    text-align: center;
    line-height: 50px;
    font-size: 14px;
  }
  .lv3-box {
    display: flex;
    flex-wrap: wrap;
  }
  .lv3-item {
    display: flex;
    flex-direction: column;
    justify-content: center;
    align-items: center;
    width: 33.33%;
    margin-bottom: 10px;
    image {
      width: 60px;
      height: 60px;
    }
    text {
      font-size: 12px;
    }
  }
}
</style>
